package cc.telecomdigital.tdstock.trading.Common;

import java.math.BigDecimal;

import android.os.Parcel;
import android.os.Parcelable;

import cc.telecomdigital.tdstock.trading.Tools.Trade_StockFunctionTools;

public class Trade_StockReqOrderItem implements Parcelable
{
	//Create once only
	public static final String	TRANSACTIONTYPE_BID	= "B";
	public static final String	TRANSACTIONTYPE_ASK	= "A";

	public String				orderNumber;
	public String				stockCode;
	public String				chnName;
	public String				engName;
	// Quantity is not used for calculation, basically it is the order that is
	// being called
	public String				quantity;					// For Display
													// only
	public String				filledQuantity;			// For Display
													// only
	public String				outStandingQuantity;		// The order
													// quantity
													// will be
													// issued
	public String				price;

	private String				totalWorth;				// outStanding
													// *price
	private String				orderType;				// E type
													// mostly
	private String				orderSide;				// Bid or Ask	

	private String                currencyType;
	
	public Trade_StockReqOrderItem()
	{
		orderType = TRANSACTIONTYPE_BID;
		totalWorth = "";
		orderType  = "E";
		currencyType = "HKD";
	}

	/** Request Order for Stock, works in TradeOrderCancel, TradeOrderModify and TradeOrderPlace **/
	public Trade_StockReqOrderItem(Trade_StockReqOrderItem iItem)
	{
		orderNumber = iItem.orderNumber;
		stockCode = iItem.stockCode;
		chnName = iItem.chnName;
		engName = iItem.engName;
		quantity = iItem.quantity;
		filledQuantity = iItem.filledQuantity;
		outStandingQuantity = iItem.outStandingQuantity;
		price = iItem.price;
		totalWorth = iItem.totalWorth;
		orderType = iItem.orderType;
		orderSide = iItem.orderSide;
		currencyType = iItem.currencyType;
	}

	/** For Modifying Order, place in the orderNumber and requested info **/
	public Trade_StockReqOrderItem(String iOrderNumber, String iCode,
			String iChnName, String iEngName)
	{
		orderNumber = iOrderNumber;
		stockCode = iCode;
		chnName = iChnName;
		engName = iEngName;
		orderSide = TRANSACTIONTYPE_BID;
		totalWorth = "";
		currencyType = "HKD";
	}

	/** For Placing Order, place in the order code and other info **/
	public Trade_StockReqOrderItem(String iCode, String iChnName,
			String iEngName)
	{
		stockCode = iCode;
		chnName = iChnName;
		engName = iEngName;
		orderSide = TRANSACTIONTYPE_BID;
		totalWorth = "";
		currencyType = "HKD";		
	}

	/** Reset the order if needed **/
	public void ResetStockOrderItem(String iCode, String iChnName,
			String iEngName)
	{
		stockCode = iCode;
		chnName = iChnName;
		engName = iEngName;
		totalWorth = "";
		currencyType = "HKD";		
		quantity = null;
		price = null;
	}

	/** Set OrderSide, either "B" or "A" (Bid / Ask) **/
	public void SetOrderSide(String iOrderSide)
	{
		orderSide = iOrderSide;
	}

	/** Place in the order quantity info, information from TradeOrdered **/
	public void SetOrderQuantity(String iQuantity, String iFillQuantity,
			String iOutStandQuantity)
	{
		quantity = iQuantity;
		filledQuantity = iFillQuantity;
		SetOutStandingOrderQuantity(iOutStandQuantity);
	}

	/** Set the order quantitiy **/
	public void SetOutStandingOrderQuantity(String iQuantity)
	{
		if (iQuantity == null)
			iQuantity = "";

		BigDecimal dValue;

		if (iQuantity.length() == 0)
			quantity = "0";
		else
		{
			dValue = (new BigDecimal(iQuantity.replace(",", "")))
					.setScale(0);
			outStandingQuantity = dValue.toString();
		}

		totalWorth = CalculateTotal(price, outStandingQuantity);
		totalWorth = Trade_StockFunctionTools
				.AutoFormatPriceToCents(totalWorth);
	}

	/** Set Order Type, default is "E" **/
	public void SetOrderType(String iOrderType)
	{
		orderType = iOrderType;
	}

	/** Set the desire price for ordering, the total will calculated automatically once
	 * quantity is also placed **/
	public void SetOrderPrice(String iPrice)
	{
		if (iPrice == null)
			iPrice = "";

		if (iPrice.length() == 0)
			price = "0";
		else
			price = iPrice.replace(",", "");

		totalWorth = CalculateTotal(price, outStandingQuantity);
		totalWorth = Trade_StockFunctionTools
				.AutoFormatPriceToCents(totalWorth);
	}

	public String getOrderType()
	{
		return orderType;
	}
	
	public void SetCurrencyCode(String iCurrency)
	{
		currencyType = iCurrency;
	}
	
	public String GetCurrencyCode()
	{
		return currencyType;
	}

	public String getOrderSide()
	{
		return orderSide;
	}

	/** Get the price in formated string with "," for every thousand **/
	public String getPiceText()
	{
		String tmpString;

		tmpString = Trade_StockFunctionTools.AutoFillInDecimal(price,
				Trade_AppConstant.priceValueStringWithDec);
		return Trade_StockFunctionTools.AddFormatToThousand(tmpString);
	}

	/** Get the Outstanding Quantitiy in formatted string with "," for every thousand **/
	public String getOutStandingQuantityText()
	{
		return Trade_StockFunctionTools
				.AddFormatToThousand(outStandingQuantity);
	}

	/** Get Filled Quantitiy in formatted string with "," for every thousand **/
	public String getFilledQuantityText()
	{
		return Trade_StockFunctionTools.AddFormatToThousand(filledQuantity);
	}

	/** Get the quantitiy in formated string with "," for every thousand **/
	public String getQuantityText()
	{
		return Trade_StockFunctionTools.AddFormatToThousand(quantity);
	}

	/** Get the totalworth in formatted string with "," for every thousand **/
	public String getTotalText()
	{
		return Trade_StockFunctionTools.AddFormatToThousand(totalWorth);
	}

	/** Recalculate the total and return in formatted string with "," for every thousand **/
	public static String CalculateTotal(String iPrice, String iQuantity)
	{
		return Trade_StockFunctionTools.CalculateProductInPrice(iPrice,
				iQuantity);
	}

	/** Recalculate the total and return in formatted string with "," for every thousand or no format **/	
	public static String CalculateTotalText(String iPrice, String iQuantity,
			boolean iFormat)
	{
		String valueString;

		valueString = CalculateTotal(iPrice, iQuantity);
		if (valueString == null || valueString.equals("")
				|| valueString.equals("0"))
			return "0.00";

		return Trade_StockFunctionTools.AddFormatToThousand(valueString
				.toString());
	}

	static private int parcelStringMember = 12;	
	/** Parcelable function call **/
	@Override
	public void writeToParcel(Parcel out, int iFlag)
	{
		// Write has the same sequence of read		
		out.writeStringArray(new String[]{ orderNumber, stockCode, chnName,
								     engName, quantity, filledQuantity,
									outStandingQuantity, price, currencyType, 
									totalWorth, orderType, orderSide
								   });
	}

	public static final Parcelable.Creator<Trade_StockReqOrderItem>	CREATOR	= new Parcelable.Creator<Trade_StockReqOrderItem>()
																{
																	public Trade_StockReqOrderItem createFromParcel(
																			Parcel in)
																	{
																		return new Trade_StockReqOrderItem(
																				in);
																	}

																	public Trade_StockReqOrderItem[] newArray(
																			int size)
																	{
																		return new Trade_StockReqOrderItem[size];
																	}
																};

	private Trade_StockReqOrderItem(Parcel in)
	{
		String[] inStrArray;
		
		inStrArray = new String[parcelStringMember];		
		in.readStringArray(inStrArray);
		
		orderNumber = inStrArray[0];
		stockCode = inStrArray[1];
		chnName = inStrArray[2];
		engName = inStrArray[3];
		quantity = inStrArray[4];
		filledQuantity = inStrArray[5];
		outStandingQuantity = inStrArray[6];
		price = inStrArray[7];
		currencyType = inStrArray[8];
		totalWorth = inStrArray[9];
		orderType = inStrArray[10];
		orderSide = inStrArray[11];
	}

	@Override
	public int describeContents()
	{
		return 0;
	}
}
